package com.fy56.platform.goodsTracking.police.quartz;

import com.fy56.platform.goodsTracking.commons.DateUtil;
import com.fy56.platform.goodsTracking.jdbc.datasource.DBManage;
import com.fy56.platform.goodsTracking.model.PoliceSet;
import com.fy56.platform.goodsTracking.model.SysCompany;
import com.fy56.platform.goodsTracking.police.api.PoliceUploadApi;
import com.fy56.platform.goodsTracking.police.service.PoliceSetService;
import com.fy56.platform.goodsTracking.police.service.SysCompanyService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by XTK on 2017-02-28.
 */
@Service
public class PoliceUpload {

    static Logger log = Logger.getLogger(PoliceUpload.class);
    @Autowired
    private PoliceSetService policeSetService;
    @Autowired
    private DBManage dbManage;
    @Autowired
    private PoliceUploadApi uploadApi;
    @Autowired
    private SysCompanyService sysCompanyService;

    public void upload() {
        int iloop = 0;
        //log.info("执行公安厅上传操作(" + DateUtil.Now() + ")");
        //查询本地数据库设置
        //查询所有需要上传的组织代码
        List<PoliceSet> policeSetList = policeSetService.list();
        for (PoliceSet policeSet : policeSetList) {
            log.info("执行公安厅上传操作:Organizationcode:" + policeSet.getOrganizationcode()
                    + ";---开始---");
            try {
                dbManage.setDBLink_upload(policeSet.getOrganizationcode());//切换数据库
                //查询公司信息
                List<SysCompany> sysCompanies=sysCompanyService.list(policeSet.getOrganizationcode());
                //上传网点信息
                uploadApi.doUpload_Company(policeSet,sysCompanies);
                //上传物品类型
                uploadApi.doUpload_Goods(policeSet);
                //上传业务类型
                uploadApi.doUpload_Product(policeSet);
                //上传订单
                uploadApi.doUpload_ConsignmentBillMaster(policeSet,sysCompanies);
                //运单业务类型数据信息
            } catch (Exception e) {
                log.info("执行公安厅上传操作:Organizationcode:" + policeSet.getOrganizationcode()
                        + ";---错误---" + e.getMessage());
            }
        }

        //根据
        try {
            Thread.sleep(10000);//休息一会
        } catch (InterruptedException ire) {
        }
    }
}
